using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player_Ctrl : Singleton_Base<Player_Ctrl>
{
    public override bool isDontDestroy => false;
    public  Unit_Charactor_Ctrl charactor_Ctrl;

    [SerializeField]
    private List<int> directionList=new List<int>();
    [SerializeField]
    private Vector2 moveJoyStickDirection;
    public bool isReach;
    public Vector3 reachDirection;
    public bool isThrowState;
    public float fullReachTime;
    public bool isJumped;
    [Range(0,1f)]
    public float reachMaxJoystickDistance=0.5f;

    public DrawParabola_Ctrl drawParabola_Ctrl;

    bool isTouchKeyBoard = false;
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.P))
        {
            charactor_Ctrl.mpMax++;
            charactor_Ctrl.mp = charactor_Ctrl.mpMax;
        }

        if (Input.GetKeyDown(KeyCode.A))
        {
            directionList.Add(-1);
            isTouchKeyBoard = true;
        }
        if (Input.GetKey(KeyCode.A))
        {
            isTouchKeyBoard = true;
        }
        if (Input.GetKeyUp(KeyCode.A))
        {
            directionList.Remove(-1);
            isTouchKeyBoard = true;
        }
        if (Input.GetKeyDown(KeyCode.D))
        {
            directionList.Add(1);
            isTouchKeyBoard = true;
        }
        if (Input.GetKey(KeyCode.D))
        {
            isTouchKeyBoard = true;
        }
        if (Input.GetKeyUp(KeyCode.D))
        {
            directionList.Remove(1);
            isTouchKeyBoard = true;
        }
        if (Input.GetKey(KeyCode.S))
        {
            charactor_Ctrl.dropDirection = -1;
        }
        if (Input.GetKeyUp(KeyCode.S))
        {
            charactor_Ctrl.dropDirection = 0;
        }


        Vector3 targetMousePos = Input.mousePosition;
        targetMousePos.z = 10;
        Vector3 mouseWorldPos = Camera.main.ScreenToWorldPoint(targetMousePos);
        mouseWorldPos.z = 0;
        if (charactor_Ctrl != null)
        {
            this.transform.position = charactor_Ctrl.transform.position;
            //if (Input.GetMouseButton(0))
            //{
            //    charactor_Ctrl.VirtualReachHand(mouseWorldPos - charactor_Ctrl.hand_Case.transform.position);
            //}
            if (Input.GetKeyDown(KeyCode.Space))
            {
                charactor_Ctrl.Jump();
            }
        }
   

    }
    private void FixedUpdate()
    {
        charactor_Ctrl.FixedRun();
        if (isTouchKeyBoard)
        {
            if (directionList.Count > 0)
            {
                charactor_Ctrl.Move(directionList[directionList.Count - 1]);
            }
            else
            {
                charactor_Ctrl.Idle();
            }
        }
        else
        {
            if (moveJoyStickDirection.magnitude > 0)
            {
                charactor_Ctrl.Move((moveJoyStickDirection.x>0? Mathf.CeilToInt(moveJoyStickDirection.x / 0.5f) : Mathf.FloorToInt(moveJoyStickDirection.x / 0.5f) )* 0.5f);
                charactor_Ctrl.dropDirection = moveJoyStickDirection.y;
                if (moveJoyStickDirection.y >= 0.5f)
                {
                    charactor_Ctrl.Jump();
                    if (!isJumped)
                    {
                        isJumped = true;
                    }
                }
                if (moveJoyStickDirection.y < 0.1f)
                {
                    isJumped = false;
                }
            }
            else
            {
                charactor_Ctrl.Idle();
            }
        }
        isTouchKeyBoard = false;
    }
    private void LateUpdate()
    {

        //Camera_Ctrl.instance.Run();
    }
    public void Init()
    {
        UpJoystick(Vector3.zero);
        UpLeftJoystick(Vector2.zero);
        moveJoyStickDirection = Vector2.zero;
        directionList.Clear();
        drawParabola_Ctrl.gameObject.SetActive(false);
        isReach = false;
        isThrowState = false;
        charactor_Ctrl.Init();

    }
    public void OnMoveDown(int direction)
    {
        //if (directionList.Contains(direction))
        //{
        //    directionList.Remove(direction);
        //}
        //directionList.Add(direction);
        moveJoyStickDirection.x = direction;
    }
    public void OnMoveUp(int direction)
    {
        //if (directionList.Contains(direction))
        //{
        //    directionList.Remove(direction);
        //}
        moveJoyStickDirection.x = 0;
    }
    public void OnJump()
    {
        charactor_Ctrl.Jump();
    }
    public void OnLight()
    {
        charactor_Ctrl.EmitLight();
    }
    public float GetThrowTByJoysitckDistance(float distance)
    {
        float t = ((distance - reachMaxJoystickDistance) / (1 - reachMaxJoystickDistance));
        return t;
    }
    public void DownJoystick(Vector3 direction)
    {
        isReach = true;
        reachDirection = direction;
        isThrowState = false;
        fullReachTime = 0;
    }
    public void DragJoystick(Vector3 direction)
    {
        isReach= direction.magnitude >= 0.1f;
        reachDirection = direction;
    }
    public void UpJoystick(Vector3 direction)
    {
        if (isReach)
        {
            if (!isThrowState)
            {
            }
            else
            {
                drawParabola_Ctrl.gameObject.SetActive(false);
            }
        }
        fullReachTime = 0;
        isReach = false;
    }

    public void DownLeftJoystick(Vector3 direction)
    {

    }
    public void DragLeftJoystick(Vector3 direction)
    {
        moveJoyStickDirection = direction;
    }
    public void UpLeftJoystick(Vector3 direction)
    {
        moveJoyStickDirection = Vector3.zero;
        isJumped = false;
        directionList.Clear();
    }

}
